﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using ProtoBuf.Message;

namespace UI
{
    public class ShowGet : MonoBehaviour
    {
        public GameObject commonIcon;
        public GameObject scrollView;
        public SimpleGrid simpleGrid;
        public UILabel labTitle;

        List<GameObject> dropItems = new List<GameObject>();

        public void SetGets(List<BaseResource> objs,string title="")
        {

            foreach (var obj in dropItems)
                Destroy(obj);
            dropItems.Clear();
            foreach (var info in objs)
            {
                var obj = GameObject.Instantiate(commonIcon) as GameObject;
                obj.GetComponent<CommonIcon>().SetData(info.id, false, info.num);
                obj.transform.SetParent(simpleGrid.transform);
                obj.transform.localPosition = Vector3.zero;
                obj.transform.localScale = Vector3.one;
                obj.transform.localEulerAngles = Vector3.zero;
                simpleGrid.AddChild(obj);
                dropItems.Add(obj);
                obj.SetActive(true);
            }

            simpleGrid.Reposition();

            //标题
            if (title == "")
                labTitle.text = TableTextBlo.GetText("congratulateGot");
            else
                labTitle.text = title;

            GetComponent<PageTweener>().Play();
        }
        
        public void SetGets(ResultObject objs, string title = "", bool isHidePanel = true)
        {
            foreach (var item in simpleGrid.childs)
            {
                Destroy(item);
            }
            simpleGrid.childs.Clear();

            
            //hero
            foreach(var item in objs.wars)
            {
                CreateCommonIcon(item.id, 1, isHidePanel);
            }

            //equip
            foreach (var item in objs.equips)
            {
                CreateCommonIcon(item.equipId, 1, isHidePanel);
            }

            //treasure
            foreach (var item in objs.treas)
            {
                CreateCommonIcon(item.treasureId, 1, isHidePanel);
            }

            //simpleGoods 不显示精力体力
            foreach (var item in objs.simps)
            {
                if(item.id != Dict.Config.TableConst.energy && item.id != Dict.Config.TableConst.stamina && item.num>0)
                    CreateCommonIcon(item.id, item.num, isHidePanel);
            }

            //标题
            if (title == "")
                labTitle.text = TableTextBlo.GetText("congratulateGot");
            else
                labTitle.text = title;

            GetComponent<PageTweener>().Play();
        }

        void CreateCommonIcon(string id,int num,bool isHidePanel = true)
        {
            GameObject icon = Instantiate(commonIcon);
            icon.SetActive(true);
            icon.GetComponent<CommonIcon>().SetData(id,true,num);
            if(isHidePanel)
                icon.AddComponent<UIEventListener>().onClick = this.OnCommonIconClick;
            icon.AddComponent<UIDragScrollView>().scrollView = scrollView.GetComponent<UIScrollView>();
            simpleGrid.AddChild(icon);
        }

        void OnCommonIconClick(GameObject obj)
        {
            OnConfirmBtn();
        }

        public void OnConfirmBtn()
        {
            gameObject.SetActive(false);
            Destroy(this.gameObject);
        }

    }
}
